﻿@using System.Configuration
@using System.Data.SqlClient
@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Export Claim Page";

    string frm_filter_entries = string.Empty;
    string sql_filter = string.Empty;
    
    bool hasError = false;
    string errorMessage = string.Empty;
    bool hasSuccess = false;
    string successMessage = string.Empty;

    System.Data.DataSet ds = new System.Data.DataSet("CurrentClaims");
    List<KeppelLand.Entities.Claimant> claimantList = null;
    List<KeppelLand.Entities.Claim> claimList = null;
    KeppelLand.Entities.ClaimSummary claimSummary = null;
    List<KeppelLand.Entities.ClaimPeriod> claimPeriodList = null;

    if (IsPost)
    {
        if (Request.Form["actionButton"] == "filter")
        {
            frm_filter_entries = Request.Form["filter_entries"];
            string[] entries = frm_filter_entries.Split(',');
            if (entries.Length > 0)
            {
                frm_filter_entries = "[";
                foreach (string en in entries)
                {
                    frm_filter_entries = frm_filter_entries + "'" + en + "',";

                    if (!string.IsNullOrEmpty(en.Trim()))
                    {
                        sql_filter = sql_filter + string.Format("{0},", en.Trim());
                    }
                }
                frm_filter_entries = frm_filter_entries.Remove(frm_filter_entries.Length - 1, 1);
                frm_filter_entries = frm_filter_entries + "]";
                if (sql_filter.Length > 0)
                {
                    sql_filter = sql_filter.Remove(sql_filter.Length - 1, 1);
                }
            }
        } // if (Request.Form["actionButton"] == "filter")
        
        if (Request.Form["actionButton"] == "download")
        {
            byte[] fileBytes = KeppelLand.Controllers.ReportController.GetClaimHistoryReport(
                Request.Form["download_format"],
                KeppelLand.Helpers.UserIdentityHelper.GetClaimantName(),
                KeppelLand.Entities.ClaimStatus.EXPORT_TO_SAP);
            Response.Clear();
            Response.ContentType = KeppelLand.Helpers.TranslationHelper.GetContentType(Request.Form["download_format"]);
            Response.AddHeader("content-disposition", "attachment; filename=" + KeppelLand.Helpers.TranslationHelper.GetFileName(Request.Form["download_format"], "Claim-History"));
            Response.BinaryWrite(fileBytes);
            Response.Flush();
            Response.End();
        } // if (Request.Form["actionButton"] == "download")
    } // if (IsPost)

    claimantList = KeppelLand.Controllers.ClaimController.GetClaimantListSubmittedToSupervisor(KeppelLand.Helpers.UserIdentityHelper.GetClaimantName());
    claimList = KeppelLand.Controllers.ClaimController.GetClaimHistoryList(KeppelLand.Helpers.UserIdentityHelper.GetClaimantName(), KeppelLand.Entities.ClaimStatus.EXPORT_TO_SAP, sql_filter);
    claimSummary = KeppelLand.Controllers.ClaimController.GetClaimHistorySummary(KeppelLand.Helpers.UserIdentityHelper.GetClaimantName(), KeppelLand.Entities.ClaimStatus.EXPORT_TO_SAP, sql_filter);
    claimPeriodList = KeppelLand.Controllers.ClaimController.GetClaimPeriodList(KeppelLand.Helpers.UserIdentityHelper.GetClaimantName(), KeppelLand.Entities.ClaimStatus.EXPORT_TO_SAP);
}

@if (!KeppelLand.Helpers.UserIdentityHelper.IsTrainer())
{
    @RenderPage("~/Shared/Access_Control.cshtml");
    return;
}

<div class="container-fluid main-content" role="main">

    @RenderPage("~/Shared/Help_Tab.cshtml")

    @if (hasError)
    {
        <div class="alert alert-danger alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            @Html.Raw(errorMessage)
        </div>
    }
    @if (hasSuccess)
    {
        <div class="alert alert-success alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            @successMessage
        </div>
    }

    <form method="post" class="hidden-print">

        <h2>Claim History</h2>

    </form>

    <form method="post" id="filter_form">

        <table class="table table-striped table-bordered">
            <tbody>
                <tr>
                    <th class="col-lg-2" style="vertical-align:middle;">
                        <span class="glyphicon glyphicon-filter" aria-hidden="true"></span> User Filter
                    </th>
                    <td class="col-lg-10">
                        <select id="filter_entryowner">
                            <option value="">All</option>
                            @foreach (KeppelLand.Entities.Claimant claimant in claimantList)
                            {
                                <option value="@claimant.EmployeeMaster">@DisplayHelper.LegalNameByEmployeeMaster(claimant.EmployeeMaster)</option>
                            }
                        </select>
                    </td>
                </tr>
            </tbody>
        </table>

        <input type="hidden" name="filter_entries" id="filter_entries" />
        <input type="hidden" name="actionButton" id="filter_form_action" />
    </form>

    @RenderPage("~/Shared/Employee_Claim_History.cshtml", claimList, claimSummary)

    <form method="post" id="approval_form">
        <input type="hidden" name="actionButton" id="actionButton" />
        <input type="hidden" name="actionId" id="actionId" />
    </form>

    <p>&nbsp;</p>

</div>

@RenderPage("~/Shared/Standard_Modals.cshtml")

<div class="modal fade" id="messageModal" tabindex="-1" role="dialog" aria-labelledby="messageModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="messageModalLabel">Message</h4>
            </div>
            <div class="modal-body" id="messageModalBody">
                <p>
                    <strong>Rejection Reason:</strong>
                </p>
                <p id="messageModal_rejected_reason">
                    Click OK to proceed with this action.<br />
                    Click Canel to abort this action.
                </p>

                <p>
                    <strong>Rejected By</strong><br />
                    <span id="messageModal_rejected_by"></span>
                </p>
                <p>
                    <strong>Rejected Date</strong><br />
                    <span id="messageModal_rejected_date"></span>
                </p>
            </div>

            <div class="modal-footer">

                <button type="button" class="btn btn-primary" id="messageModal_OK" data-dismiss="modal">OK</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="helpModal" tabindex="-1" role="dialog" aria-labelledby="helpModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="helpModalLabel">Help <small>(Claim History)</small></h4>
            </div>
            <div class="modal-body">
                <p>
                    Use this page to view claims submitted to Finance by HR.<br />
                </p>
                <!-- 
                <div class="panel panel-default">
                    <div class="panel-heading">Instructions to add new claim</div>
                    <div class="panel-body">
                        <ol>
                            <li>Enter claim date, official hours and overtime hours. These are required fields.</li>
                            <li>
                                Fill in overtime hours if applicable.<br />
                                This is required if you intend to claim OT or meal allowances.
                            </li>
                        </ol>
                    </div>
                </div>

                -->


            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="helpModal_Close" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

@section Scripts
{
    <script type="text/javascript" src="~/Scripts/kendo/2014.3.1119/kendo.ui.core.min.js"></script>
    <script type="text/javascript" src="~/Scripts/moment.js"></script>
    <script type="text/javascript" src="~/Scripts/moment-with-locales.js"></script>
    <script type="text/javascript">
    function download(format) {
        $("#approval_form #actionButton").val("download");
        $("#approval_form #download_format").val(format);
        $("#approval_form").submit();
    }

    (function ($) {
        "use strict";
        $(document).ready(function () {
            //$("#filter_entryowner").kendoMultiSelect();
            var multi = $("#filter_entryowner").kendoMultiSelect({
                change: function (e) {
                    $("#filter_entries").val(this.value());
                    $("#filter_form_action").val("filter");
                    $("#filter_form").submit();
                }
            }).data("kendoMultiSelect");
            multi.value(@Html.Raw(frm_filter_entries));
        });
    }(jQuery));
    </script>
}